#include "playscene.h"
#include <QMenuBar>
#include <QPainter>
#include <QTimer>
#include <QLabel>
#include <QPropertyAnimation>
#include <QSoundEffect>
#include "mypushbutton.h"
#include "mycoin.h"
#include "dataconfig.h"

PlayScene::PlayScene(int levelNum)
{
    this->levelIndex=levelNum;
    //初始化场景
    setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("游戏场景");
    //创建菜单栏
    QMenuBar * bar = menuBar();
    setMenuBar(bar);
    //创建开始菜单
    QMenu *startMenu = bar->addMenu("开始");
    //创建退出菜单项
    QAction * quitAction = startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    QSoundEffect * filpSound = new QSoundEffect();
    filpSound->setSource(QUrl::fromLocalFile(":/res/ConFlipSound.wav"));
    QSoundEffect * backSound = new QSoundEffect();
    backSound->setSource(QUrl::fromLocalFile(":/res/BackButtonSound.wav"));
    QSoundEffect * winSound = new QSoundEffect();
    winSound->setSource(QUrl::fromLocalFile(":/res/LevelWinSound.wav"));
    //返回按钮
    MyPushButton *backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    connect(backBtn,&MyPushButton::clicked,[=](){
        //告诉关卡返回
        backSound->play();
        QTimer::singleShot(500,this,[=](){
            emit this->choseSceneBack();
        });
    });
    //显示当前关卡
    QFont font;
//    font.setFamily("华文新魏");
    font.setBold(true);
    font.setPointSize(20);
    QLabel * label=new QLabel(this);
    label->setParent(this);
    label->setFont(font);
    label->setText(QString("Level：%1").arg(this->levelIndex));
    label->setGeometry(QRect(30,this->height()-50,120,50));
    //初始化每个关卡的二维数组
    dataConfig config;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
        }
    }
    //显示胜利图片
    QLabel * winLabel=new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())*0.5,-tmpPix.height());
    //显示金币背景图
    for(int i =0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            QLabel * label=new QLabel;
            label->setGeometry(0,0,50,50);
            label->setPixmap(QPixmap(":/res/BoardNode.png"));
            label->setParent(this);
            label->move(57+i*50,200+j*50);
            //创建金币
            QString str;
            if(this->gameArray[i][j]==1)
            {
                str=":/res/Coin0001.png";
            }else{
                str=":/res/Coin0008.png";
            }
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(59+i*50,204+j*50);
            //给金币属性复制
            coin->posX=i;
            coin->posY=j;
            coin->flag=this->gameArray[i][j];//1正面0反面
            this->btnCoin[i][j]=coin;
            //点击金币
            connect(coin,&MyCoin::clicked,[=](){
                filpSound->play();
                //将所有按钮禁用
                for(int i=0;i<4;i++)
                {
                    for(int j=0;j<4;j++)
                    {
                        this->btnCoin[i][j]->isWin=true;
                    }
                }
                coin->changeFlag();
                this->gameArray[i][j]=this->gameArray[i][j]==0?1:0;
                //翻转周围金币
                QTimer::singleShot(300,this,[=](){
                    if(coin->posX+1<4)
                    {
                        //右边
                        this->btnCoin[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]==0?1:0;
                    }
                    if(coin->posX-1>-1)
                    {
                        //右边
                        this->btnCoin[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]==0?1:0;
                    }
                    if(coin->posY-1>-1)
                    {
                        //上边
                        this->btnCoin[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]==0?1:0;
                    }
                    if(coin->posY+1<4)
                    {
                        //上边
                        this->btnCoin[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]==0?1:0;
                    }
                    this->isWin=true;
                    for(int i=0;i<4;i++)
                    {
                        for(int j=0;j<4;j++)
                        {
                            if(this->btnCoin[i][j]->flag==false)
                            {
                                this->isWin=false;
                            }
                            this->btnCoin[i][j]->isWin=false;
                        }
                    }
                    if(this->isWin)
                    {
                        //胜利
                        for(int i=0;i<4;i++)
                        {
                            for(int j=0;j<4;j++)
                            {
                                this->btnCoin[i][j]->isWin=true;
                            }
                        }
                        //将胜利图片移动下来
                        QPropertyAnimation * animation = new QPropertyAnimation(winLabel,"geometry");
                        winSound->play();
                        animation->setDuration(1000);
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+184,winLabel->width(),winLabel->height()));
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        animation->start();
                    }
                });

            });
        }
    }

}
void PlayScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    ///加载标题///
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()*.5,pix.height()*.5);
    //绘制
    painter.drawPixmap(10,30,pix);
    //
}
